package com.tjcd.project.library_base.common.upload.presenter;


import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tjcd.project.library_base.base.bean.BaseHttpBean;
import com.tjcd.project.library_base.common.baseBean.OssUploadFileBean;
import com.tjcd.project.library_base.common.upload.bean.CommonFileBean;
import com.tjcd.project.library_base.common.upload.bean.UploadBean;
import com.tjcd.project.library_base.common.upload.contract.FileUploadContract;
import com.tjcd.project.library_base.common.upload.model.FileUploadModel;
import com.tjcd.project.library_base.common.upload.net.BaseFileObserver;
import com.tjcd.project.library_base.common.upload.net.BasePresenter;
import com.tjcd.project.library_base.common.upload.progress.FileUploadObserver;

import java.io.File;


public class FileUploadPresenter extends BasePresenter<FileUploadContract.View, FileUploadModel> implements FileUploadContract.Presenter {
    @Override
    protected void initModel() {
        setModel(new FileUploadModel());
    }


    @Override
    public void fileUploadOss(File file, String type) {
        mModel.fileUploadOss(file, type, new BaseFileObserver<BaseHttpBean<OssUploadFileBean>>() {

            @Override
            public void onSuc(BaseHttpBean<OssUploadFileBean> ossUploadFileBeanBaseDataBean) {
                if (ObjectUtils.isNotEmpty(ossUploadFileBeanBaseDataBean.getData())) {
                    mView.fileUploadOssSuc(ossUploadFileBeanBaseDataBean.getData(), file, type);
                } else {
                    mView.fileUploadOssFail(file, type);
                }
            }

            @Override
            public void onFail(String msg) {
                mView.fileUploadOssFail(file, type);
            }
        });
    }


    @Override
    public void fileUpload(File file, String type) {
        mModel.fileUpload(file, type, new BaseFileObserver<BaseHttpBean<UploadBean>>() {

            @Override
            public void onSuc(BaseHttpBean<UploadBean> uploadBean) {
                if (ObjectUtils.isNotEmpty(uploadBean.getData())) {
                    mView.fileUploadSuc(uploadBean.getData(), type);
                } else {
                    mView.fileUploadFai(type);
                    ToastUtils.showLong("上传失败");
                }
            }

            @Override
            public void onFail(String msg) {
                mView.fileUploadFai(type);
                ToastUtils.showLong(msg);
            }

            @Override
            public void onLoadStart() {
                super.onLoadStart();
//                LoadingUtils.LoadShow(mContext);
            }

            @Override
            public void onLoadFinished() {
                super.onLoadFinished();
//                LoadingUtils.LoadDismiss(mContext);
            }
        });
    }

    @Override
    public void fileUploadProgress(File file, String type, FileUploadObserver inter) {
        mModel.fileUploadProgress(file, type, inter, new BaseFileObserver<BaseHttpBean<UploadBean>>() {

            @Override
            public void onSuc(BaseHttpBean<UploadBean> uploadBean) {
                if (ObjectUtils.isNotEmpty(uploadBean.getData())) {
                    mView.fileUploadSuc(uploadBean.getData(), type);
                } else {
                    mView.fileUploadFai(type);
                    ToastUtils.showLong("上传失败");
                }
            }

            @Override
            public void onFail(String msg) {
                mView.fileUploadFai(type);
                ToastUtils.showLong(msg);
            }

            @Override
            public void onLoadStart() {
                super.onLoadStart();
            }

            @Override
            public void onLoadFinished() {
                super.onLoadFinished();
            }
        });
    }

    @Override
    public void fileUploadProgress(File file, String type, final CommonFileBean bean) {
        mModel.fileUpload(file, type, new BaseFileObserver<BaseHttpBean<UploadBean>>() {

            @Override
            public void onSuc(BaseHttpBean<UploadBean> uploadBean) {
                if (ObjectUtils.isNotEmpty(uploadBean.getData())) {
                    bean.setComplete(true);
                    bean.setUrl(uploadBean.getData().getUrl());
                    mView.fileUploadSuc(uploadBean.getData(), type);
                } else {
                    mView.fileUploadFai(type);
                    ToastUtils.showLong("上传失败");
                }
            }

            @Override
            public void onFail(String msg) {
                mView.fileUploadFai(type);
                ToastUtils.showLong(msg);
            }

            @Override
            public void onLoadStart() {
                super.onLoadStart();
            }

            @Override
            public void onLoadFinished() {
                super.onLoadFinished();
            }
        });
    }
}
